/**
 * ShoppingCartDemo
 * 作者Git：https://github.com/guangqiang-liu
 * 技术交流群：620792950
 * 作者QQ：1126756952
 * Created by guangqiang on 2017/11/5.
 */

import React, {Component, PureComponent} from 'react'
import {
    View,
    Text,
    TouchableOpacity,
    Image,
    StyleSheet,
    SectionList,
    Dimensions,
    SafeAreaView,
    FlatList
} from 'react-native'
import {commonStyle} from './commonStyle'

const shoppingCartData = require('./ShoppingCartData.json')
const {width, height} = Dimensions.get('window')
import EmptyCouponView from "../../../widget/CommonEmptyView";
import CommonAlertDialog from "../../../widget/dialog/CommonAlertDialog";
import api from '../../../api/api'
import method from '../../../api/method'
import NetTool from '../../../api/NetTool'
import {fetchGetRequest, fetchPostRequest} from "../../../api/NetworkTool";
import UseCouponDialog from '../../../widget/dialog/UseCouponDialog'
import Tips from 'react-native-root-tips'
import ShoppingOrderList from "./ShoppingOrderList";
import DeviceStorage from "../../../utils/DeviceStorage";
import define from "../../../utils/define";
import {ArrayTool} from '../../../utils/arrayExtension'
import {NavigationActions} from "react-navigation";

var co = require('co')

type Props = {
    navigation: any,
}
let token = ''
DeviceStorage.get(define.token).then((args) => {
    if (args) {
        token = 'Bearer ' + args;
    }
})
var flag = 0
export default class ShoppingCart extends PureComponent<Props> {

    static navigationOptions = ({navigation}: any) => ({
        headerTintColor: 'black',
        title: '购物车',
        headerStyle: {backgroundColor: 'white'},
        headerRight: <Text/>,
        headerLeft: <TouchableOpacity onPress={() => {
            navigation.goBack()
        }}>
            <Image style={{marginLeft: 12}} source={require('../../../../res/img/gobal_back.png')}/>
        </TouchableOpacity>,

    })

    constructor(props) {
        super(props)
        this.renderItem = this.renderItem.bind(this)
        this.state = {
            total: 0,
            status: [],
            editStatus: [],
            isSelectedAllItem: false,
            isSelectedAllItemDelete: false,
            totalPrice: 0.00,
            isEdit: false,
            isShow: false,
            couponData: [],
            selectIndex: -1,
            youhuiPrice: 0,
            show_alert: false,
            address: null,

        }
        this.gobalCoupon = []
        this.couponId = -1
    }


    mapData(res) {
        let datalist = res.course.map((info) => {
            return {
                id: info.id,
                title: info.title,
                start_date: info.start_date,
                end_date: info.end_date,
                teacher: info.teacher,
                tag: info.tag,
                subject: info.subject,
                end_time: info.end_time,
                frequency: info.frequency,
                price: info.price,
                start_time: info.start_time,
            }
        })
        return datalist

    }

    componentWillMount() {
        this.setState({status: [], editStatus: []})

    }

    componentDidMount() {
        this.addressRequestData()
        // 网络请求获取购物车数据
        fetchGetRequest(api.shop_list, method.get,)
            .then(res => {

                console.log('resss', res)

                let dataList = this.mapData(res)

                let tempArr = []
                for (let i = 0; i < dataList.length; i++) {
                    let item = dataList[i]
                    item.checked = false
                    item.deleteChecked = false
                    item.couponTitle = '请选择优惠券'
                    item.distance = ''
                    item.distancePrice = 0
                    item.couponId = 0
                    item.deleteChecked = false
                    tempArr.push(item)
                    // let params = {
                    //     course: item.id,
                    // }
                    // fetchGetRequest(api.coupon_find, method.get, params)
                    //     .then(res1 => {
                    //         console.log('res', res1)
                    //         if (res1) {
                    //             let dataList1 = this.mapcoupondata(res1)
                    //
                    //             if (dataList1.length > 0) {
                    //                 let data = this.filterArrayData(dataList1)
                    //                 let coupon = data[0]
                    //                 item.couponTitle = coupon.title
                    //                 item.distance = '此券可用，去结算立减' + coupon.price + '元'
                    //                 item.distancePrice = coupon.price
                    //                 item.couponId = coupon.id
                    //                 this.gobalCoupon.push(coupon.id)
                    //                 item.deleteChecked = false
                    //
                    //                 tempArr.push(item)
                    //
                    //                 this.setState({status: tempArr, editStatus: tempArr,})
                    //
                    //             } else {
                    //                 item.couponTitle = '请选择优惠券'
                    //                 item.distance = ''
                    //                 item.distancePrice = 0
                    //                 item.couponId = 0
                    //                 item.deleteChecked = false
                    //
                    //                 tempArr.push(item)
                    //
                    //                 this.setState({status: tempArr, editStatus: tempArr,})
                    //
                    //             }
                    //         }
                    //     })
                    //     .catch(err => {
                    //     })

                }

                this.setState({status: tempArr, editStatus: tempArr,})
            })
            .catch(err => {

            })
    }


    checkItem(index) {

        let tempStatus = this.state.status
        let tempShop = tempStatus[index]
        tempShop.checked = !tempShop.checked

        let isSelectedAllShopItem = true
        for (let j = 0; j < tempStatus.length; j++) {
            let item = tempStatus[j]
            if (!item.checked) {
                isSelectedAllShopItem = false
                break
            }
        }
        this.calculateCountAndPrice()
        this.setState({status: tempStatus, isSelectedAllItem: isSelectedAllShopItem})


    }

    checkItemEdit(index) {

        let tempStatus = this.state.editStatus
        let tempShop = tempStatus[index]
        tempShop.deleteChecked = !tempShop.deleteChecked

        // alert('ssss',tempShop.isDelete)

        console.log('fff', tempStatus)


        let isSelectedAllShopItem = true
        for (let j = 0; j < tempStatus.length; j++) {
            let item = tempStatus[j]
            if (!item.deleteChecked) {
                isSelectedAllShopItem = false
                break
            }
        }

        this.calculateCountAndPriceEdit()
        this.setState({editStatus: tempStatus, isSelectedAllItemDelete: isSelectedAllShopItem})


    }

    checkAllShop() {
        let tempSelectedAllItem = !this.state.isSelectedAllItem
        let tempStatus = this.state.status
        for (let i = 0; i < tempStatus.length; i++) {
            let shop = tempStatus[i]
            shop.checked = tempSelectedAllItem
        }


        this.calculateCountAndPrice()
        this.setState({isSelectedAllItem: tempSelectedAllItem, status: tempStatus})
    }

    checkAllShopEdit() {
        let tempSelectedAllItem = !this.state.isSelectedAllItemDelete
        let tempStatus = this.state.editStatus
        for (let i = 0; i < tempStatus.length; i++) {
            let shop = tempStatus[i]
            shop.deleteChecked = tempSelectedAllItem
        }


        this.setState({isSelectedAllItemDelete: tempSelectedAllItem, editStatus: tempStatus})
    }

    calculateCountAndPrice() {
        let tempTotalPrice = 0
        let tempStatus = this.state.status
        let youhuiPrice = 0
        for (let j = 0; j < tempStatus.length; j++) {
            let item = tempStatus[j]
            if (item.checked) {
                if ((item.price - item.distancePrice) > 0) {
                    let price =  (item.price - item.distancePrice)
                    tempTotalPrice += price
                    console.log('price',price, item.price , item.distancePrice,tempTotalPrice)
                    youhuiPrice += item.distancePrice
                }else {
                    tempTotalPrice += 0
                    youhuiPrice += item.price
                }
            }
        }
        console.log('price',tempTotalPrice)
        this.setState({totalPrice: tempTotalPrice, youhuiPrice: youhuiPrice})
    }

    calculateCountAndPriceEdit() {
        let tempTotalPrice = 0
        let tempStatus = this.state.editStatus
        for (let j = 0; j < tempStatus.length; j++) {
            let item = tempStatus[j]
            if (item.deleteChecked) {
                tempTotalPrice += item.price
            }
        }
        this.setState({total: tempTotalPrice})
    }


    renderItem = info => {
        let course = info.item
        let index = info.index
        let start_time = course.start_date.substr(5, 5).replace('-', '月') + '日'
        let end_time = course.end_date.substr(5, 5).replace('-', '月') + '日'
        let mas_ta = course.teacher[0]
        let tou_ta = course.teacher[1]

        return (
            <View style={{backgroundColor: 'white'}}>
                <View style={{marginLeft: 12, marginRight: 12, backgroundColor: 'white'}}>
                    <TouchableOpacity onPress={() => this.checkItem(index)}   style={{flexDirection: 'row', alignItems: 'center', justifyContent: 'center'}}>
                        <View  style={{
                            marginRight: 12,
                            marginLeft: -10,
                            marginTop: 12,
                            justifyContent: 'center',
                            alignItems: 'center',
                            width: 30,
                            height: 40
                        }}>
                            <Image style={{width: 10, height: 10}} source={course.checked ?
                                require('../../../../res/img/detail/detail_select.png')
                                : require('../../../../res/img/detail/detail_un_select.png')}/>
                        </View>

                        <View>
                            <View style={{
                                flexDirection: 'row',
                                backgroundColor: 'white',
                                marginTop: 10,
                                alignItems: 'center'
                            }}>
                                <Text numberOfLines={1} style={styles.title}>{course.title}</Text>
                            </View>
                            <View style={{
                                flexDirection: 'row',
                                backgroundColor: 'white',
                                marginTop: 10,
                                alignItems: 'center',
                            }}>
                                <Image style={{width: 12, height: 12}}
                                       source={require('../../../../res/img/select/select_time.png')}/>
                                <Text
                                    style={[styles.time, {marginLeft: 7}]}>{start_time + '到' + end_time + course.frequency + course.start_time + '-' + course.end_time}</Text>
                            </View>
                            <View style={{
                                flexDirection: 'row',
                                backgroundColor: 'white',
                                marginTop: 8,
                                alignItems: 'flex-end',
                            }}>
                                <View style={{flexDirection: 'row', alignItems: 'center',}}>
                                    <Image style={{width: 28, height: 28, borderRadius: 14}}
                                           source={{uri: mas_ta.image}}/>
                                    <View style={{marginLeft: 10, justifyContent: 'center', height: 28}}>
                                        <Text style={styles.teacher}>讲师</Text>
                                        <Text style={styles.teacher_show}>{mas_ta.realname}</Text>
                                    </View>
                                </View>
                                <View style={{flexDirection: 'row', alignItems: 'center', marginLeft: 30}}>
                                    <Image style={{width: 28, height: 28, borderRadius: 14}}
                                           source={{uri: tou_ta.image}}/>
                                    <View style={{marginLeft: 10, justifyContent: 'center', height: 28}}>
                                        <Text style={styles.teacher}>辅导</Text>
                                        <Text style={styles.teacher_show}>{tou_ta.realname}</Text>
                                    </View>
                                </View>
                                <View style={{flex: 1}}/>
                                <View style={{flexDirection: 'row', alignItems: 'center'}}>
                                    <Image style={{width: 7, height: 8, marginTop: 7}}
                                           source={require('../../../../res/img/select/select_price.png')}/>
                                    <Text style={styles.price}>{course.price}</Text>
                                </View>
                            </View>

                        </View>
                    </TouchableOpacity>
                    <TouchableOpacity style={{marginTop: 10, flex: 1, height: 24, width: width, marginBottom: 15}}
                                      onPress={() => {
                                          this.couponId = course.couponId
                                          this.getCouponData(course)
                                          this.setState({selectIndex: index})
                                      }}>
                        <Image style={{width: (width - 24), height: 24, position: 'absolute'}}
                               source={require('../../../../res/img/detail/order_course_bg.png')}/>
                        <View style={{marginTop: 9, flexDirection: 'row', paddingLeft: 8}}>
                            <Text style={{color: '#DB3843', fontSize: 10, marginTop: 1}}>{course.couponTitle}</Text>
                            <Text style={{color: '#333333', fontSize: 10, marginLeft: 8}}>{course.distance}</Text>
                        </View>
                    </TouchableOpacity>
                </View>
            </View>
        )
    }
    renderCell = (info: any) => {
        let course = info.item
        let index = info.index

        let start_time = course.start_date.substr(5, 5).replace('-', '月') + '日'
        let end_time = course.end_date.substr(5, 5).replace('-', '月') + '日'
        let mas_ta = course.teacher[0]
        let tou_ta = course.teacher[1]
        return (
            <View style={{backgroundColor: 'white'}}>
                <View style={{marginLeft: 12, marginRight: 12, backgroundColor: 'white'}}>
                    <TouchableOpacity onPress={() => this.checkItemEdit(index)} style={{flexDirection: 'row', alignItems: 'center', justifyContent: 'center'}}>
                        <View style={{
                            marginRight: 12,
                            marginLeft: -10,
                            marginTop: 12,
                            justifyContent: 'center',
                            alignItems: 'center',
                            width: 30,
                            height: 40
                        }}>
                            <Image style={{width: 10, height: 10}} source={course.deleteChecked ?
                                require('../../../../res/img/detail/detail_select.png')
                                : require('../../../../res/img/detail/detail_un_select.png')}/>
                        </View>

                        <View>
                            <View style={{
                                flexDirection: 'row',
                                backgroundColor: 'white',
                                marginTop: 10,
                                alignItems: 'center'
                            }}>
                                <Text numberOfLines={1} style={styles.title}>{course.title}</Text>
                            </View>
                            <View style={{
                                flexDirection: 'row',
                                backgroundColor: 'white',
                                marginTop: 10,
                                alignItems: 'center',
                            }}>
                                <Image style={{width: 12, height: 12}}
                                       source={require('../../../../res/img/select/select_time.png')}/>
                                <Text
                                    style={[styles.time, {marginLeft: 7}]}>{start_time + '到' + end_time + course.frequency + course.start_time + '-' + course.end_time}</Text>
                            </View>
                            <View style={{
                                flexDirection: 'row',
                                backgroundColor: 'white',
                                marginTop: 8,
                                alignItems: 'flex-end',
                            }}>
                                <View style={{flexDirection: 'row', alignItems: 'center',}}>
                                    <Image style={{width: 28, height: 28, borderRadius: 14}}
                                           source={{uri: mas_ta.image}}/>
                                    <View style={{marginLeft: 10, justifyContent: 'center', height: 28}}>
                                        <Text style={styles.teacher}>讲师</Text>
                                        <Text style={styles.teacher_show}>{mas_ta.realname}</Text>
                                    </View>
                                </View>
                                <View style={{flexDirection: 'row', alignItems: 'center', marginLeft: 30}}>
                                    <Image style={{width: 28, height: 28, borderRadius: 14}}
                                           source={{uri: tou_ta.image}}/>
                                    <View style={{marginLeft: 10, justifyContent: 'center', height: 28}}>
                                        <Text style={styles.teacher}>辅导</Text>
                                        <Text style={styles.teacher_show}>{tou_ta.realname}</Text>
                                    </View>
                                </View>
                                <View style={{flex: 1}}/>
                                <View style={{flexDirection: 'row', alignItems: 'center'}}>
                                    <Image style={{width: 7, height: 8, marginTop: 7}}
                                           source={require('../../../../res/img/select/select_price.png')}/>
                                    <Text style={styles.price}>{course.price}</Text>
                                </View>
                            </View>

                        </View>
                    </TouchableOpacity>
                    <View style={{marginTop: 10, flex: 1, height: 24, width: width, marginBottom: 15}}>
                        <Image style={{width: (width - 24), height: 24, position: 'absolute'}}
                               source={require('../../../../res/img/detail/order_course_bg.png')}/>
                        <View style={{marginTop: 9, flexDirection: 'row', paddingLeft: 8}}>
                            <Text style={{color: '#DB3843', fontSize: 10, marginTop: 1}}>{course.couponTitle}</Text>
                            <Text style={{color: '#333333', fontSize: 10, marginLeft: 8}}>{course.distance}</Text>
                        </View>
                    </View>
                </View>
            </View>
        )
    }

    mapcoupondata(res) {
        let dataList = res.coupon.map((info) => {
            return {
                id: info.id,
                title: info.title,
                count: info.count,
                price: info.price,
                status: info.status,
                start_datetime: info.start_datetime,
                end_datetime: info.end_datetime,
                coupon_sn: info.coupon_sn,
            }
        })


        return dataList
    }


    filterArrayData(dataList) {
        var gobalCoupon = this.gobalCoupon

        for (let i = dataList.length - 1; i >= 0; i--) {
            let kkitem = dataList[i]
            gobalCoupon.forEach(id => {
                if (kkitem.id == id) {
                    dataList.splice(i, 1)
                }
            })
        }

        return dataList
    }

    getCouponData(course) {

        this.setState({isShow: true})

        let params = {
            course: course.id,
        }
        fetchGetRequest(api.coupon_find, method.get, params)
            .then(res => {
                let dataList = this.mapcoupondata(res)

                let data = this.filterArrayData(dataList)
                this.setState({couponData: data})
            })
            .catch(err => {
            })
    }


    addressRequestData = async () => {

        let json = await NetTool.fetchGetRequest(api.getUserAddr, method.get)

        if (json.addrList.length > 0) {
            let address = json.addrList[0]

            this.setState({address: address})
        } else {
            this.setState({address: null})
        }

    }

    renderBootom() {
        if (this.state.status.length == 0) {
            return <View/>
        } else if (this.state.isEdit) {
            return <View style={{
                width: width,
                height: 62,
                backgroundColor: 'white',
                flexDirection: 'row',
                alignItems: 'center'
            }}>
                <TouchableOpacity
                    style={{ width: 50, height: 40, flexDirection: 'row', alignItems: 'center'}}
                    onPress={() => {
                        this.checkAllShopEdit()
                    }}>
                    <View style={{width:30,height:1}}/>
                    <Image style={{width: 10, height: 10}}
                           source={this.state.isSelectedAllItemDelete ? require('../../../../res/img/detail/detail_select.png') : require('../../../../res/img/detail/detail_un_select.png')}/>
                    <Text style={{marginLeft: 7, color: '#333333', fontSize: 12}}>全选</Text>
                </TouchableOpacity>
                <View style={{flex: 1}}/>

                <TouchableOpacity onPress={() => {
                    var isChecked = false
                    this.state.editStatus.forEach(info => {
                        if (info.deleteChecked) {
                            isChecked = true
                        }
                    })
                    if (!isChecked) {
                        Tips.showInfo('请选择需要删除的课程', {mask: true})
                    } else {
                        this.setState({show_alert: true})
                    }
                }} style={{
                    marginLeft: 12,
                    backgroundColor: '#DB3843',
                    width: 92,
                    height: 34,
                    borderRadius: 17,
                    marginRight: 10,
                    justifyContent: 'center',
                    alignItems: 'center'
                }}>
                    <Text style={{color: 'white', fontSize: 14, fontWeight: 'bold'}}>删除</Text>
                </TouchableOpacity>
            </View>
        } else {
            return <View style={{
                width: width,
                height: 62,
                backgroundColor: 'white',
                flexDirection: 'row',
                alignItems: 'center'
            }}>
                <TouchableOpacity onPress={() => this.checkAllShop()} style={{
                    width: 50,
                    height: 40,
                    flexDirection: 'row',
                    alignItems: 'center'
                }}>
                    <View style={{width:30,height:1}}/>
                    <Image style={{width: 10, height: 10}}
                           source={this.state.isSelectedAllItem ? require('../../../../res/img/detail/detail_select.png') : require('../../../../res/img/detail/detail_un_select.png')}/>
                    <Text style={{marginLeft: 7, color: '#333333', fontSize: 12}}>全选</Text>
                </TouchableOpacity>
                <View style={{flex: 1}}/>
                <View style={{justifyContent: 'center'}}>
                    <View style={{flexDirection: 'row', alignItems: 'center'}}>
                        <Text style={{color: 'rgba(0,0,0,0.6)', fontSize: 12, fontWeight: 'bold'}}>应付金额</Text>
                        <Text style={{
                            marginLeft: 12,
                            color: '#DB3843',
                            fontSize: 16,
                            fontWeight: '700'
                        }}>¥ {this.state.totalPrice}</Text>
                    </View>
                    <View style={{flexDirection: 'row', alignItems: 'center', marginTop: 5}}>
                        <Text style={{color: '#010101', fontSize: 12, fontWeight: 'bold'}}>优惠金额</Text>
                        <Text style={{
                            color: '#DB3843',
                            fontSize: 16,
                            fontWeight: '700',
                            marginLeft: 12
                        }}>¥ {this.state.youhuiPrice}</Text>

                    </View>
                </View>
                <TouchableOpacity style={{
                    marginLeft: 12,
                    backgroundColor: '#DB3843',
                    width: 92,
                    height: 34,
                    borderRadius: 17,
                    marginRight: 10,
                    justifyContent: 'center',
                    alignItems: 'center'
                }} onPress={() => {
                    var courses = '['
                    this.state.status.forEach(info => {
                        if (info.checked) {
                            let str = '(' + info.id + ',' + info.couponId + '),'
                            courses += str
                        }
                    })
                    if (courses == '[') {
                        Tips.showInfo('请选择课程', {mask: true})
                        return
                    }
                    if (!this.state.address) {
                        Tips.showInfo('请选择地址', {mask: true})
                        return
                    }
                    courses += ']'
                    let params = {
                        productId: courses,
                    }
                    fetchPostRequest(api.createOrder, method.post, params)
                        .then(res => {
                            this.props.navigation.navigate("ShoppingOrderList", {res: res})
                        })
                        .catch(err => {
                        })


                }}>
                    <Text style={{color: 'white', fontSize: 14, fontWeight: 'bold'}}>立即结算</Text>
                </TouchableOpacity>
            </View>
        }

    }

    renderHeader() {


        var address = '请选择地址'

        if (this.state.address) {
            address = this.state.address.city_name + this.state.address.address
        }

        return <View>
            <View style={{
                width: width,
                height: 46,
                alignItems: 'center',
                flexDirection: 'row',
                backgroundColor: 'white'
            }}>
                <TouchableOpacity style={{flexDirection: 'row', alignItems: 'center'}} onPress={() => {
                    fetchGetRequest(api.getUserAddr, method.get)
                        .then(res => {
                            if (res.addrList.length > 0) {
                                this.props.navigation.navigate('MineAddressScene', {
                                    isOrder: true, callback: (data) => {
                                        this.addressRequestData()
                                    }
                                })
                            } else {
                                this.props.navigation.navigate('AddAddressScene', {
                                    isOrder: true, callback: (data) => {
                                        this.addressRequestData()
                                    }
                                })
                            }
                        })
                        .catch(err => {
                        })
                }}>
                    <Image style={{width: 22, height: 18, marginLeft: 12}}
                           source={require('../../../../res/img/ic_avatar_default.png')}/>
                    <Text numberOfLines={1} style={{
                        color: '#333333',
                        fontSize: 12,
                        marginLeft: 18,
                        width: (width - 124)
                    }}>{address}</Text>
                </TouchableOpacity>
                <View style={{flex:1}}/>
                <TouchableOpacity style={{marginRight: 12, height: 44, justifyContent: 'center', alignItems: 'center',}}
                                  onPress={() => {
                                      this.setState({isEdit: !this.state.isEdit})
                                  }}>
                    {this.state.isEdit?<Text style={{color: '#DB3843', fontSize: 12}}>完成</Text>:<Text style={{color: '#DB3843', fontSize: 12}}>编辑</Text>}
                </TouchableOpacity>
            </View>
            <View style={{width: width, height: 1, backgroundColor: 'rgba(0,0,0,0.1)'}}/>
        </View>

    }

    render() {
        if (this.state.status.length == 0) {
            return <SafeAreaView
                style={{backgroundColor: '#F9F9F9', flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                <Image style={{width: 46, height: 43}} source={require('../../../../res/img/shop/shop_empty.png')}/>
                <Text style={{color: '#999999', fontSize: 12, marginTop: 15}}>购物车空空如也</Text>
                <TouchableOpacity onPress={() => {
                    //重置根视图，闪屏页=>主界面后销毁掉
                    const resetAction = NavigationActions.reset({
                        index: 0,
                        actions: [NavigationActions.navigate({routeName: 'home'})]
                    })
                    this.props.navigation.dispatch(resetAction)
                }} style={{
                    backgroundColor: '#DB3843',
                    borderRadius: 3,
                    width: 72,
                    height: 25,
                    marginTop: 15,
                    justifyContent: 'center',
                    alignItems: 'center'
                }}>
                    <Text style={{color: '#FFFFFF', fontSize: 12,}}>去选课</Text>
                </TouchableOpacity>
            </SafeAreaView>
        } else {
            return (
                <SafeAreaView style={styles.container}>
                    {this.renderHeader()}
                    {this.state.isEdit ? <FlatList
                            renderItem={this.renderCell}
                            data={this.state.editStatus}
                            keyExtractor={this.keyExtractor1}
                            ItemSeparatorComponent={() => <View
                                style={{
                                    width: width - 24,
                                    height: 1,
                                    backgroundColor: 'rgba(0,0,0,0.1)',
                                    marginLeft: 12
                                }}/>}
                        /> :
                        <FlatList
                            renderItem={this.renderItem}
                            data={this.state.status}
                            keyExtractor={this.keyExtractor}
                            ItemSeparatorComponent={() => <View
                                style={{
                                    width: width - 24,
                                    height: 1,
                                    backgroundColor: 'rgba(0,0,0,0.1)',
                                    marginLeft: 12
                                }}
                            />}/>

                    }
                    <UseCouponDialog data={this.state.couponData} show={this.state.isShow} closeModal={(show) => {
                        this.setState({isShow: show})
                    }} callBack={(info) => {
                        if (this.couponId >= 0){
                            for (let i = this.gobalCoupon.length - 1; i >= 0; i--) {
                                let kkitem = this.gobalCoupon[i]
                                if (kkitem == this.couponId) {
                                    this.gobalCoupon.splice(i, 1)
                                }

                            }
                        }


                        let distinct = '此券可用，去结算立减' + info.price + '元'
                        let item = this.state.status[this.state.selectIndex]
                        item.couponTitle = info.title
                        item.distance = distinct
                        item.distancePrice = info.price
                        item.couponId = info.id
                        let gobalCoupon = this.gobalCoupon
                        gobalCoupon.push(info.id)
                        this.gobalCoupon = gobalCoupon
                        this.calculateCountAndPrice()

                    }} cancelPress={() => {
                        let item = this.state.status[this.state.selectIndex]
                        for (let i = this.gobalCoupon.length - 1; i >= 0; i--) {
                            let kkitem = this.gobalCoupon[i]
                            if (kkitem == item.couponId) {
                                this.gobalCoupon.splice(i, 1)
                            }
                        }
                        item.couponTitle = '请选择优惠券'
                        item.distance = ''
                        item.distancePrice = 0
                        item.couponId = 0
                        this.calculateCountAndPrice()
                    }}/>
                    <CommonAlertDialog title={'确定删除课程？'} onPress={()=>{
                        this.confirmOpress()
                    }} closeModal={
                        (st)=>{this.setState({show_alert:st})}
                    } confirmTitle={'删除'} show={this.state.show_alert}/>
                    {this.renderBootom()}
                </SafeAreaView>
            )
        }
    }


    confirmOpress() {
        let dataList = this.state.editStatus
        var obj = ''

        dataList.forEach(info => {
            if (info.deleteChecked) {
                let str = info.id + ','
                obj += str
            }
        })
        if (obj.length > 0) {
            let params = {
                course: '[' + obj + ']',
            }
            fetchPostRequest(api.shop_delete,method.post, params)
                .then(res=>{
                   let dataList = this.deleteShop()
                    this.calculateCountAndPrice()
                    this.calculateCountAndPriceEdit()
                    this.setState({status:dataList,editStatus:dataList})
                })
                .catch(err=>{

                })
        }

    }
    deleteShop(){
        let dataList = this.state.editStatus
        for (let i = dataList.length - 1; i >= 0; i--) {
            let kkitem = dataList[i]
            if (kkitem.deleteChecked) {
                dataList.splice(i, 1)
            }
        }
        return dataList
    }

    keyExtractor = (item: Object, index: number) => {
        return index + ''
    }
    keyExtractor1 = (item: Object, index: number) => {
        return index * item.id + 'rr'
    }

    modifyCart = () => {

    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5F5F5',
        width: width,
    },
    title: {
        fontWeight: 'bold',
        color: 'rgba(0,0,0,0.8)',
        fontSize: 16,
        width: (width - 100),
    },
    time: {
        fontSize: 12,
        color: '#999999',
        fontWeight: '200',
    },
    teacher: {
        fontSize: 9,
        color: '#333333',
    },
    teacher_show: {
        fontSize: 9,
        color: '#999999',
        marginTop: 4,
    },
    price: {
        color: 'rgba(197, 50, 60, 1)',
        fontSize: 19,
        fontWeight: '700',
        marginLeft: 6,
    }
})